package main

import (
	"encoding/json"
	"fmt"
	"iron_man_comm/src/goroslib"
	"iron_man_comm/src/goroslib/dns"
	"iron_man_comm/src/srv/common_msg"
	"iron_man_comm/src/tools/log"
	"time"
)

type DataStruct struct {
	TimeStamp    int64  `json:"timestamp"`
	DeviceUpData string `json:"device_up_data"`
	Data         string `json:"data"`
}

func main() {
	log.InitializeLog()
	dns.GetDNSCacheInterface().UpdateDNSMapping("rk3562-buildroot", "127.0.0.1")
	node, err := goroslib.NewNode(goroslib.NodeConf{
		Name:          fmt.Sprintf("report_device_data_%d", time.Now().UnixMilli()),
		MasterAddress: fmt.Sprintf("%s:11311", "127.0.0.1"),
	})
	if err != nil {
		log.Error.Println("create node fail, err: %s", err)
		return
	}
	defer node.Close()

	client, err := goroslib.NewServiceClient(goroslib.ServiceClientConf{
		Node:            node,
		Name:            "/user/data/up/srv",
		Srv:             &common_msg.ReportDeviceData{},
		EnableKeepAlive: false,
	})
	if err != nil {
		return
	}
	defer client.Close()

	data := DataStruct{
		TimeStamp:    time.Now().UnixMilli(),
		DeviceUpData: "123",
		Data:         "wifi_config",
	}

	bytes, err := json.Marshal(data)
	if err != nil {
		log.Error.Printf("json marshal fail, err: %s", err.Error())
		return
	}

	req := common_msg.ReportDeviceDataReq{
		Data: string(bytes),
	}
	res := common_msg.ReportDeviceDataRes{}

	err = client.Call(&req, &res)
	if err != nil {
		log.Error.Printf("call fail")
		return
	}
	log.Info.Println("call pass")
	log.Info.Println("res: ", res.ErrorCode)
}
